package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.commons.entity.FeedType;
import com.campus.commons.entity.Feedback;
import com.campus.app.mapper.FeedbackMapper;
import com.campus.app.service.FeedbacksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 问题反馈 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbacksService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Result<List<Feedback>> getAllFeedbacks(MyPage<Feedback> myPage) {
        return Result.result(feedbackMapper.selectPage(new Page<Feedback>(myPage.getPage(),myPage.getLimit()),getQueryNewest()));
    }

    @Override
    public Result delFeedback(Integer id) {
        Feedback feedback = feedbackMapper.getOne(id);
        if (feedback.getState()!=Feedback.State_Complete) {
            return Result.error(Error.Operation_E1R, "该反馈还未处理，不能执行删除操作");
        }
        return Result.result(feedbackMapper.deleteById(id));
    }

    @Override
    public Result<List<Feedback>> getFeedbacks(MyPage<Feedback> myPage) {
        QueryWrapper queryWrapper = new QueryWrapper();
         Feedback data = myPage.getData();
        queryWrapper.eq(data.getState()!=null,"state", data.getState());
        String name = data.getFeedType().getName();
        Integer isSystem = data.getFeedType().getIsSystem();
        List<Integer> collect = FeedType.feeds.values().stream().filter(f -> {
            // 三种情况： 一种都没有
            boolean b = true;
            if (name == null && isSystem == null) {
                return false;
            }
            if (name != null && isSystem != null) {
                return b = name.equals(f.getName()) && isSystem.intValue() == f.getIsSystem();
            }
            if (name != null) {
                return b = name.equals(f.getName());
            }
            if (isSystem != null) {
                return b = isSystem.intValue() == f.getIsSystem();
            }
            return b;
        }).map(feedType -> {
            return feedType.getId();
        }).collect(Collectors.toList());
        queryWrapper.in(name!=null|| isSystem !=null
                ,"feedTypeId", collect);
      return   Result.result(feedbackMapper.selectPage(new Page<Feedback>(myPage.getPage(), myPage.getLimit()), queryWrapper));
//        return Result.result(feedbackMapper.getFeeds(Tool.pageConverterToOffset(myPage)));
    }

    @Override
    public Result addFeedback(Feedback feedback) {

        return Result.result(feedbackMapper.insert(feedback));
    }

    @Override
    public Result disposeFeedback(Integer id) {
        Feedback feedback = feedbackMapper.getOne(id);
        if (feedback==null) {
        return     Result.error(Error.PARAMS_E3R, "找不到该Id的反馈信息");
        }
        if (feedback.getState() ==Feedback.State_Complete) {
            return Result.error(Error.PARAMS_E3R, "该反馈已经处理过了，不能重复操作");
        }
        feedback.setState(feedback.getState()+1);
        return Result.result(feedbackMapper.updateById(feedback));
    }

    @Override
    public Result<Feedback> getFeedbackById(Integer id) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.verify(feedbackMapper.selectOne(queryWrapper));
    }

    /**
     * 返回时间最新，并且未处理反馈
     * @return
     */
    private QueryWrapper<Feedback> getQueryNewest() {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("state");
        queryWrapper.orderByDesc("createTime");
        return queryWrapper;
    }
}
